package com.yonyou.iuap.controller.pc;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yonyou.iuap.pub.JsonUtils;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.intf.IProductService;

/**
 * 产品表
 * */
@Controller
@RequestMapping("/pcBdproduct")
public class PCBdproductController {

	private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());
	private long start,end;
	@Autowired
	private IProductService iProductService;

	/**
	 * 产品查所有
	 * **/
	@RequestMapping(value = "pc/queryAll", method = RequestMethod.POST)
	public @ResponseBody String LikeIKnowledgeCategory(@RequestBody Map map,HttpServletRequest request,HttpServletResponse response) {
		start=System.currentTimeMillis();
		ResultDto rsdto = new ResultDto();

		rsdto = iProductService.selectAll();
		String rs_json = JsonUtils.toJsonStr(rsdto);
		end=System.currentTimeMillis();
		logger.info("/pcBdproduct/queryAll接口调用时间"+String.valueOf(end-start));
		return rs_json;
	}
	
	/**
	 * 查询产品
	 * {"type":"2","pk_industry_chart":"10","name":"Ga"}
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/queryProduct", method = RequestMethod.POST)
	public @ResponseBody String queryProduct(@RequestBody Map param,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		ResultDto rsdto = new ResultDto();
		//1查询所有，2根据关键字查询，3模糊查询
		String type=param.get("type")==null?"":param.get("type").toString();
		if(param.get("pageNum")!=null)
			param.put("pageNum", (((int)param.get("pageNum"))-1)*((int)param.get("pageSize")));
		if("1".equals(type)){
			rsdto = iProductService.selectAll();
		}else if("2".equals(type)){
			rsdto = iProductService.queryProduct(param);
		}else if("3".equals(type)){
			if(StringUtils.isEmpty(param.get("name"))){
				rsdto.setMessage("参数不能为空");
				rsdto.setStatus(1);
			}else{
				String sql="upper(product_name) LIKE upper('%"+param.get("name")+"%')";
				rsdto = iProductService.selectByCondition(sql);
			}
			
		}
		
		String rs_json = JsonUtils.toJsonStr(rsdto);
		end=System.currentTimeMillis();
		logger.info("/pcBdproduct/queryProduct接口调用时间"+String.valueOf(end-start));
		return rs_json;
	}
	
	
	
	
	/**
	 * 查询产品  分页
	 * {"type":"2","pk_industry_chart":"10","name":"Ga"}
	 * @param param
	 * @return
	 */
	@RequestMapping(value = "/queryProductPage", method = RequestMethod.POST)
	public @ResponseBody String queryProductPage(@RequestBody Map param,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		ResultDto rsdto = new ResultDto();
		//1查询所有，2根据关键字查询，3模糊查询
		String type=param.get("type")==null?"":param.get("type").toString();
		if(param.get("pageNum")!=null)
		param.put("pageNum", (((int)param.get("pageNum"))-1)*((int)param.get("pageSize")));
		if("1".equals(type)){
			rsdto = iProductService.selectAllPage(param);
		}else if("2".equals(type)){
			rsdto = iProductService.pageQueryProduct(param);
		}else if("3".equals(type)){
			if(StringUtils.isEmpty(param.get("name"))){
				rsdto.setMessage("参数不能为空");
				rsdto.setStatus(1);
			}else{
				
				rsdto = iProductService.selectByConditionPage(param);
			}
			
		}
		
		String rs_json = JsonUtils.toJsonStr(rsdto);
		end=System.currentTimeMillis();
		logger.info("/pcBdproduct/queryProductPage接口调用时间"+String.valueOf(end-start));
		return rs_json;
	}

}
